package com.daxt.controller.sysarchives;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.archives.ArchivesDataMapper;
import com.daxt.mapper.sys.DicMapper;
import com.daxt.model.service.archives.vo.ArchivesData;
import com.daxt.model.service.base.vo.Dic;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "档案类型分类管理")
@RestController
public class ArchivesDataNodeController {
	
	@Autowired
	private ArchivesDataMapper archivesDataMapper;
	@Autowired
	private DicMapper dicMapper;
	
	
	@ApiOperation(value = "查询", notes = "") 
	@GetMapping("/archivesData/{typeId}") 
	public Result<List<ArchivesData>> findAll(@PathVariable("typeId")String typeId){
		QueryWrapper<ArchivesData> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("typeId",typeId);
		List<ArchivesData> list = archivesDataMapper.selectList(queryWrapper);
		List<ArchivesData> data=new ArrayList<>();
		
		getData(list, data);
		List<ArchivesData> collect = data.stream().sorted(Comparator.comparing(ArchivesData::getSort)).collect(Collectors.toList());
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null,collect);
	}
	
	@ApiOperation(value = "增加数据节点", notes = "") 
	@PostMapping("/archivesData") 
	public Result<String> saveData(@RequestBody ArchivesData archivesData){
		String dataColumn = archivesData.getDataColumn();
		if(StringUtils.isEmpty(dataColumn)) {
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "请选择数据节点", null);
		}
		QueryWrapper<Dic> queryWrapper=new QueryWrapper<Dic>();
		queryWrapper.eq("dicCode", dataColumn);
		List<Dic> dicList = dicMapper.selectList(queryWrapper);
		if(dicList!=null&&dicList.size()>0) {
			archivesData.setDataColumnComment(dicList.get(0).getDicValue());
			archivesData.setDataColumn(dataColumn);
			archivesData.setDataValue(dicList.get(0).getDicValue());
			if(archivesData.getSort()==null)
				archivesData.setSort(1);
			archivesDataMapper.insert(archivesData);	
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null,null);
	}
	
	@ApiOperation(value = "修改", notes = "") 
	@PutMapping("/archivesData") 
	public Result<String> editData(@RequestBody ArchivesData archivesData){
		if(archivesData.getSort()==null)
			archivesData.setSort(1);
		archivesDataMapper.updateById(archivesData);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null,null);
	}
	
	@ApiOperation(value = "删除", notes = "") 
	@DeleteMapping("/archivesData") 
	@Transactional(rollbackFor = Exception.class)
	public Result<String> delData(@RequestBody List<String> ids){
		try {
		QueryWrapper<ArchivesData> queryWrapper=new QueryWrapper<ArchivesData>();
		queryWrapper.in("parentId", ids);
		queryWrapper.select("id");
		List<ArchivesData> selectList = archivesDataMapper.selectList(queryWrapper);
		archivesDataMapper.deleteBatchIds(ids);
		if(selectList!=null&&selectList.size()>0) {
			List<Long> ids1=new ArrayList<Long>();
			for(int i=0;i<selectList.size();i++) {
				ids1.add(selectList.get(i).getId());
			}
			QueryWrapper<ArchivesData> queryWrapper1=new QueryWrapper<ArchivesData>();
			queryWrapper.in("parentId", ids1);
			queryWrapper.select("id");
			List<ArchivesData> selectList1 = archivesDataMapper.selectList(queryWrapper1);
			archivesDataMapper.deleteBatchIds(ids1);
			if(selectList1!=null&&selectList1.size()>0) {
				List<Long> ids2=new ArrayList<Long>();
				for(int i=0;i<selectList1.size();i++) {
					ids2.add(selectList1.get(i).getId());
				}
				QueryWrapper<ArchivesData> queryWrapper2=new QueryWrapper<ArchivesData>();
				queryWrapper2.in("parentId", ids2);
				queryWrapper2.select("id");
				List<ArchivesData> selectList2 = archivesDataMapper.selectList(queryWrapper2);
				archivesDataMapper.deleteBatchIds(ids2);
				if(selectList2!=null&&selectList2.size()>0) {
					List<Long> ids3=new ArrayList<Long>();
					for(int i=0;i<selectList2.size();i++) {
						ids3.add(selectList2.get(i).getId());
					}
					QueryWrapper<ArchivesData> queryWrapper3=new QueryWrapper<ArchivesData>();
					queryWrapper3.in("parentId", ids3);
					queryWrapper3.select("id");
					List<ArchivesData> selectList3 = archivesDataMapper.selectList(queryWrapper1);
					archivesDataMapper.deleteBatchIds(ids3);
					if(selectList3!=null&&selectList3.size()>0){
						List<Long> ids4=new ArrayList<Long>();
						for(int i=0;i<selectList3.size();i++) {
							ids4.add(selectList3.get(i).getId());
						}
						archivesDataMapper.deleteBatchIds(ids4);
					}
				}
			}
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null,null);
		}catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "删除失败", null);
		}
	}
	
	
	private List<ArchivesData> getData(List<ArchivesData> list,List<ArchivesData> data){
		if(data.size()==0) {
		for(int i=0;i<list.size();i++) {
			if("0".equals(list.get(i).getParentId())) {
				data.add(list.get(i));
			}
		}
		}
		for(int i=0;i<data.size();i++) {
			for(int j=0;j<list.size();j++) {
				if(data.get(i).getId().toString().equals(list.get(j).getParentId())) {
					ArchivesData dic = list.get(j);
					if(!data.get(i).getChildren().contains(dic)) {
					data.get(i).getChildren().add(dic);
					getData(list,data.get(i).getChildren());
					}
				}
			}
		}
		return data;
	}
}
